﻿namespace MagnusGrindalBakken.TextTools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public static class Library
    {
        public static int DeleteLeftToNearestUppercase(string line, int startColumn)
        {
            if (startColumn > line.Length + 1 || startColumn < 1)
            {
                throw new ArgumentOutOfRangeException("startColumn", "Start column must be a column within the line");
            }

            if (startColumn == 1)
            {
                return 0;
            }
            else if (startColumn == 2)
            {
                return 1;
            }

            int i = startColumn - 2;
            int colsTraversed = 1;
            while (i > 0)
            {
                char currentChar = line[i];
                if (Char.IsUpper(currentChar) || Char.IsWhiteSpace(currentChar))
                {
                    break;
                }

                i--;
                colsTraversed++;
            }

            return colsTraversed;
        }

        public static int DeleteRightToNearestUppercase(string line, int startColumn)
        {
            if (startColumn > line.Length + 1 || startColumn < 1)
            {
                throw new ArgumentOutOfRangeException("startColumn", "Start column must be a column within the line");
            }

            if (startColumn == line.Length + 1)
            {
                return 0;
            }
            else if (startColumn == line.Length)
            {
                return 1;
            }

            int i = startColumn;
            int colsTraversed = 1;
            while (i < line.Length)
            {
                char currentChar = line[i];
                if (Char.IsUpper(currentChar) || Char.IsWhiteSpace(currentChar))
                {
                    break;
                }

                i++;
                colsTraversed++;
            }

            return colsTraversed;
        }
    }
}
